var checkInterval;
var checkStatusInt;
var clearAlertInt;

function alertdiv(message) {
    $("#div-alert").append(message); 
    clearAlertInt = setInterval(function () {
        $("#div-alert").text("");
        window.clearInterval(clearAlertInt);
    }, 3000);
}
function setBlockUI() {   
    $.blockUI({
        message: "Aguarde un momento por favor"
    });
    checkInterval = setInterval(function () {        
        window.clearInterval(checkInterval);
        $.unblockUI();
    }, 3000);
}

function checkVM() {
    $.ajax({
        url: '/academico/maquinaalumno/getvmstatus/',
        dataType: 'json',
        type: 'post',
        async: false,
        data: 'codigo_vm=' + $('#codigo_vm').val(),
        success: function(data) {             
            if(data.response == 'error') {                
                alertdiv('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>' +data.message+'</strong></div>');
            }
            else {
                if(data.response == 'Running') {                         
                    document.getElementById("btn-encender").disabled=true;
                    document.getElementById("btn-apagar").disabled=false;
                    document.getElementById("btn-consola").disabled=false;
                    
                } 
                else {
                    if(data.response == 'Stopped') {
                        document.getElementById("btn-encender").disabled=false;
                        document.getElementById("btn-apagar").disabled=true;
                        document.getElementById("btn-consola").disabled=true;                                            
                    } 
                    else {
                        document.getElementById("btn-encender").disabled=true;
                        document.getElementById("btn-apagar").disabled=true;
                        document.getElementById("btn-consola").disabled=true;                                            
                    }
                }
            }
            $("#lbl-estado").text(data.response);
        }    
    });
}

$(document).ready( function () {
    checkVM();
    checkStatusInt = setInterval(checkVM, 10000);
    $('#btn-apagar').click(function() {
        $.ajax({
            url: '/academico/maquinaalumno/apagarvm/',
            dataType: 'json',
            data: 'codigo_vm=' + $('#codigo_vm').val(),
            type: 'get',
            success: function(data) {
                console.log("data" + data);
                if(data.response == 'error') {                    
                    alertdiv('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error! </strong>' + data.message +'</div>');
                } else {
                    document.getElementById("btn-encender").disabled=true;
                    document.getElementById("btn-apagar").disabled=true;
                    document.getElementById("btn-consola").disabled=true;
                    setBlockUI();
                }
            },
            error: function() {                
                alertdiv('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Ocurrio un Error inesperado! </strong></div>');
            }           
        });
    });
    $('#btn-encender').click(function() {
        $.ajax({
            url: '/academico/maquinaalumno/encendervm/',
            dataType: 'json',
            data: 'codigo_vm=' + $('#codigo_vm').val(),
            type: 'get',
            success: function(data) {
                console.log("data" + data);
                if(data.response == 'error') {                    
                    alertdiv('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error! </strong>' + data.message +'</div>');
                } else {
                    if(data.response == 'warning') {                        
                        alertdiv('<div class="alert alert-warning alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Atencion! </strong>' + data.message +'</div>');
                    } else {
                        document.getElementById("btn-encender").disabled=true;
                        document.getElementById("btn-apagar").disabled=true;
                        document.getElementById("btn-consola").disabled=true;
                        setBlockUI();
                    }
                }
            },
            error: function() {                
                alertdiv('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Ocurrio un Error inesperado! </strong></div>');
            }
        });
    });
    $('#btn-consola').click(function() {
        $.ajax({
            url: '/academico/maquinaalumno/getconnparam/',
            dataType: 'json',
            data: 'codigo_vm=' + $('#codigo_vm').val(),
            type: 'get',
            success: function(data) {
                if(data.response == 'ok') {
                    urlstring = "/academico/maquinaalumno/managevm/codigo/" + data.codigo_vm;
                    console.log("URL = " + urlstring);
                    window.open(urlstring, "Consola" + data.username + "-" + data.codigo_vm);        
                }
                else {
                    alertdiv('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Error! </strong>' + data.message +'</div>');
                }
            
            },
            error: function() {                
                alertdiv('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><strong>Ocurrio un Error inesperado! </strong></div>');
            }
        });

    });    
});